package com.jungle.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;


public class GeometryUtils {


    /**
     * 地理类型转 wkt
     *
     * @param geometry
     * @return
     */
    public static String getWkt(Geometry geometry) {
        if (geometry == null) {
            return null;
        }
        WKTWriter wktWriter = new WKTWriter();
        String write = wktWriter.write(geometry);
        return write;
    }

    /**
     * wkt转地理类型
     *
     * @param wkt
     * @return
     */
    public static Geometry getGeo(String wkt) {
        if (StringUtils.isEmpty(wkt)) {
            return null;
        }
        PrecisionModel precisionModel = new PrecisionModel();

        GeometryFactory geometryFactory = new GeometryFactory(precisionModel, 4326,
                PackedCoordinateSequenceFactory.DOUBLE_FACTORY);

        WKTReader wktReader = new WKTReader(geometryFactory);

        try {
            return wktReader.read(wkt);
        } catch (Exception e) {
            throw new RuntimeException("转换wkt到GEO出错");
        }
    }


    /**
     * wkt转地理类型
     *
     * @param wkt
     * @return
     */
    public static Point getPoint(String wkt) {
        if (StringUtils.isEmpty(wkt)) {
            return null;
        }
        PrecisionModel precisionModel = new PrecisionModel();
        GeometryFactory geometryFactory = new GeometryFactory(precisionModel, 4326,
                PackedCoordinateSequenceFactory.DOUBLE_FACTORY);
        WKTReader wktReader = new WKTReader(geometryFactory);
        try {
            return wktReader.read(wkt).getCentroid();
        } catch (Exception e) {
            throw new RuntimeException("转换wkt到GEO出错");
        }
    }

    /**
     * wkt转地理类型
     *
     * @param lng 经度
     * @param lat 纬度
     * @return
     */
    public static Point getPoint(String lng, String lat) {
        if (StringUtils.isEmpty(lng) || StringUtils.isEmpty(lat)) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder("POINT (");
        stringBuilder.append(lng).append(" ").append(lat).append(")");

        PrecisionModel precisionModel = new PrecisionModel();
        GeometryFactory geometryFactory = new GeometryFactory(precisionModel, 4326,
                PackedCoordinateSequenceFactory.DOUBLE_FACTORY);
        WKTReader wktReader = new WKTReader(geometryFactory);
        try {
            return wktReader.read(stringBuilder.toString()).getCentroid();
        } catch (Exception e) {
            throw new RuntimeException("转换wkt到GEO出错");
        }
    }

    /**
     * wkt转地理类型
     *
     * @param lng 经度
     * @param lat 纬度
     * @return
     */
    public static String getWkt(String lng, String lat) {
        if (StringUtils.isEmpty(lng) || StringUtils.isEmpty(lat)) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder("POINT (");
        stringBuilder.append(lng).append(" ").append(lat).append(")");

        return stringBuilder.toString();

    }

}
